
using Youz.Common.Entity;
using Youz.Common.Helper;
using Youz.CreditRepay.DAL;
using Youz.CreditRepay.Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace Youz.CreditRepay.BLL
{
    /// <summary>
    /// Member_Config BLL Class
    /// </summary>
    public partial class Member_ConfigBLL : BaseBLL
    {
        Member_ConfigDAL dal;

        private string CacheKey = "Member_Config";
        
        public Member_ConfigBLL()
        {
            dal = new Member_ConfigDAL();
            base.ModuleName = "Member_Config";
            base.MenuName = "Member_Config";
        }

        #region  成员方法

        /// <summary>
        ///  保存一条数据
        /// </summary>
        public bool Save(Member_Config model)
        {
            model.CreateTime = DateTime.Now;
            model.Modifier = model.MemberAccount;
            model.ModifyTime = DateTime.Now;

            var r = dal.Save(model);
            
            base.WriteOperationLog(OperationType.Update, r, Core.GetJsonByObjectHaveTime(model));

            if (r)
            {
                var key = model.MemberAccount + model.ConfigKey;
                if (this.DataList.ContainsKey(key))
                {
                    this.DataList[key] = model.ConfigValue;
                }
                else
                {
                    this.DataList.Add(key, model.ConfigValue);
                }
            }
            
            return r;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Member_Config Get(String MemberAccount)
        {
            return dal.Get(MemberAccount);
        }
        
        /// <summary>
        /// 得到一个JSON对象
        /// </summary>
        public string GetJson(String MemberAccount)
        {
            return Core.GetJsonByObject(Get(MemberAccount));
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet Gets(string sqlWhere)
        {
            return dal.Gets(sqlWhere);
        }
        
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet Gets(int top, string sqlWhere, string filedOrder)
        {
            return dal.Gets(top, sqlWhere, filedOrder);
        }
        
        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string sqlWhere)
        {
            return dal.GetRecordCount(sqlWhere);
        }
        
        /// <summary>
        /// 获得数据列表分页
        /// </summary>
        public DataTable Gets(string sqlWhere, int pageindex, int pagesize, out int total, params SqlParameter[] cmdParms)
        {
            return dal.Gets(sqlWhere, pageindex, pagesize, out total, cmdParms);
        }
        #endregion  成员方法

        /// <summary>
        /// 缓存列表
        /// </summary>
        public Dictionary<string, string> DataList
        {
            get
            {
                var list = CacheHelper.GetCache(CacheKey) as Dictionary<string, string>;

                if (list == null)
                {
                    var listConfig = new EntityHelper<Member_Config>().FillEntity(Gets(""));
                    list = new Dictionary<string, string>();

                    foreach (var item in listConfig)
                    {
                        list.Add(item.MemberAccount + item.ConfigKey, item.ConfigValue);
                    }

                    CacheHelper.SetCache(CacheKey, list);
                }

                return list;
            }
        }

        /// <summary>
        /// 获得建平仓下单前确认提示
        /// </summary>
        /// <param name="memberAccount"></param>
        /// <returns></returns>
        public bool GetIsConfirmOrder(string memberAccount)
        {
            var key=memberAccount+"IsConfirmOrder";
            return DataList.ContainsKey(key) ? TypeHelper.ConvertTo<bool>(DataList[memberAccount + "IsConfirmOrder"]) : false;
        }

        /// <summary>
        /// 设置建平仓下单前确认提示
        /// </summary>
        /// <param name="memberAccount"></param>
        /// <param name="value"></param>
        public void SetIsConfirmOrder(string memberAccount, bool value)
        {
            Save(new Member_Config { MemberAccount = memberAccount, ConfigKey = "IsConfirmOrder", ConfigValue = value ? "1" : "0", Memo = "建平仓下单前确认提示" });
        }
    }
}


  